var timelineBlock = 1; 
var timelinePerPage = 10;
//var timelineSize = 0;
var checkLoadTimeline = false;
var timelineCommentPerPage = 2;
var $container;
$(document).ready(function() {
 	startPage();

	$( window ).resize(function() {
	  startPage();
	});	
	
	$('.search-icon').click(function(e) {
		$('#search_form').submit();
		
        e.preventDefault();
        e.stopPropagation();
    });
	
	loadTimeline();
	
    $.fn.is_on_screen = function () {
        var win = $(window);
        var viewport = {
            top: win.scrollTop(),
            left: win.scrollLeft()
        };
        viewport.right = viewport.left + win.width();
        viewport.bottom = viewport.top + win.height();

        var bounds = this.offset();
        bounds.right = bounds.left + this.outerWidth();
        bounds.bottom = bounds.top + this.outerHeight();

        return (!(viewport.right < bounds.left || viewport.left > bounds.right || viewport.bottom < bounds.top || viewport.top > bounds.bottom));
    };
    if ($('.timelineMore').length > 0) { 
        if ($('.timelineMore').is_on_screen()) {
	        if(checkLoadTimeline){
		        setTimeout(function(){ loadTimeline(); }, 1000);
	        }
        }
    }
    $(window).scroll(function () { 
        if ($('.timelineMore').length > 0) { 
            if ($('.timelineMore').is_on_screen()) { 
	            if(checkLoadTimeline){
		        	setTimeout(function(){ loadTimeline(); }, 1000);
	        	}
	        }
        }
    }); 

	$(window).on("orientationchange",function(){
	  activeMobile();
	  SlideMenu(0);
	});    
	
	$(document).on({
	    mouseleave: function(){
			$('.timeline-action-box').hide('blind');
	    }
    }, '.timeline-action-box');
    
	$('.action-icon1 , .action-icon2 , .action-icon3').click(function(e) {
	    $(this).find('.action-icon-staus').show('blind');
		
        e.preventDefault();
        e.stopPropagation();
    });	
	
	$(document).on({
	    mouseleave: function(){
			$(this).hide('blind');
	    }
    }, '.action-icon-staus');	
    
    if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
		$('.container').css('min-width',1000);
	}
	
});
function startPage(){
	var main_menu = $(window).height();
	$('.main_menu').css('min-height',main_menu);
	var search_box = $('.main-logo').height();
	$('.search-box').height(search_box-4);
	$('.search-icon').height(search_box-2);
}
/*
function loadTimeline(){
		var step = 0;
		var limitPage = timelinePerPage-1;
		$( ".timeLineZone div" ).remove('.clear');
		checkLoadTimeline = false;
		$('.timeLineZone .timeLineBox:hidden').each(function(i, item) {
		    var blockid = $(this).attr('data-id');
		    
			$.get( "timeline.html", { id: blockid })
				.done(function( data ) {		    
			    	$(".timeLineBox[data-id='" + blockid +"']").html(data).show().find("time.timeago").timeago();
			    	
			    	if(step == limitPage){
				    	masonry();
				    	
			    	}else{
			    		step++;
			    	}
			    });	
		    if(i >= limitPage){
		    	return false;
		    }
		});
}
*/
		//
		/*
		$.get( "timeline.html", { id: timelineBlock })
			.done(function( data ) {
				timelineBlock++;
				if((timelineBlock % timelinePerPage) != 0){
					checkLoadTimeline = false;
					loadTimeline();
					/*
					if(timelineSize == 0){
						timelineSize = 1;
						$( ".timeLineZone" ).append( "<div class=\"timeLineBox p-Relative floatL timeLineBoxLeft\" id=\"timeLine"+(timelineBlock-1)+"\">"+data+"</div>" );
					}else{
						timelineSize = 0;
						$( ".timeLineZone" ).append( "<div class=\"timeLineBox p-Relative floatL timeLineBoxRight\" id=\"timeLine"+(timelineBlock-1)+"\">"+data+"</div>" );
					}
					$( ".timeLineZone" ).append( "<div class=\"timeLineBox p-Relative floatL timeLineBoxLeft\" id=\"timeLine"+(timelineBlock-1)+"\">"+data+"</div>" );
					
					
					$("#timeLine"+(timelineBlock-1)+" time.timeago").timeago();
				}else{
					checkLoadTimeline = true;
					
					masonry();
					setTimeout(function(){ boxPosition(); }, 400);
				}
			});	
		*/

function openComment(id){
	//demo
	var box = ".timeLineBox[data-id='" + id +"']";
	$(box+' .Comment_show').hide();
	var findmax = $(box+' .Comment_tab_id').length;
	var findopen = $(box+' .Comment_tab_id:visible').length;
	var endOpen = findopen + timelineCommentPerPage;
	
	for(var i = findopen;i < endOpen; i++ ){
		$(box+' .Comment_tab_id').eq(i).fadeIn();
	}
	
	var findclose = $(box+' .Comment_tab_id:hidden').length;
	if(findclose == 0){
		$(box+' .Comment_show_more').fadeOut();
	}else{
		$(box+' .Comment_show_more').fadeIn();
	}
	masonry();

}
function SlideMenu(id){
	if(id == 1){
		$( ".main_menu" ).addClass( "main_menu_slide" );
	}else{
		$( ".main_menu" ).removeClass( "main_menu_slide" );
	}
}
function activeMobile(){
	var buttonopen = $('.mobile_menu:visible').length;
	if(buttonopen == 0){
		var documentheight = $(document).height();
		$('.main_menu').css({'max-height':documentheight , 'min-height':documentheight});
	}else{
		$('.main_menu').css({'max-height':'none' , 'min-height':'none'});
	}
}

function masonry() {

    $container = $('.timeLineZone').isotope({
	    itemSelector: '.timeLineBox'
	  });

	  /*
	  var singleAction = true;
	  $container.isotope( 'on', 'layoutComplete', function( laidOutItems ) {
		  if(singleAction){
			  singleAction = false;
			  boxPosition();
		  }
	  });
	  */	
	  setTimeout(function(){ boxPosition(); }, 400);			  
}
function boxPosition(){
			  $(".timeLineZone .timeLineBox:visible").each(function(){
			  	var position = $(this).position();
			  	if(position.left > 200){
				  	$(this).removeClass( "timeLineBoxLeft" ).addClass( "timeLineBoxRight" );
			  	}else{
				  	$(this).removeClass( "timeLineBoxRight" ).addClass( "timeLineBoxLeft" );
			  	}
			  });
					$( ".timeLineZone" ).append( "<div class=\"clear\"></div>");
					var myHeight = $('.body_content').height();
					$('.main_menu').height(myHeight+20);
					var myHeight = $('.timeLineZone').height();
					if (myHeight == 0) {
					    $('.center-line , .timelineMore').hide();
					} else {
					    $('.center-line').show().height(myHeight + 20);
					    $('.timelineMore').css('top', myHeight + 20).fadeIn(1000);
					}
					checkLoadTimeline = true;
}
function openActionBox(id){
	$('.timeline-action-box').hide(function() {
		var box = ".timeLineBox[data-id='" + id +"']";
			$(box+' .timeline-action-box').show('blind');
    });
	
}
function openDetailBox(id) {
    var box = ".timeLineBox[data-id='" + id + "']";
    $(box + ' .timeline-Detail p').removeClass('hideDetailBox');

    $(".timeLineBox[data-id='" + id + "'] p.readMoreDetailBox").hide();

    masonry();
}